#
# Copyright (C) Since 2018 ~ 2025, Inc. All Rights Reserved
#
# @Time    : 2025/1/9-上午10:18
# @Author  : Timmy
# @Email   : tangping@cloudstream-tech.com
# @File    : db.py
# @Software: lottery
# @Function:
#

import os

from tortoise import Tortoise, connections


async def init_db():
    # Here we connect to a SQLite DB file.
    # also specify the app name of "models"
    # which contain models from "app.models"

    _dir = os.path.join(os.path.expanduser("~"), '.lottery')
    if not os.path.exists(_dir):
        os.makedirs(_dir)
    db_file = os.path.join(_dir, 'db.sqlite3')

    await Tortoise.init(
        db_url=f'sqlite://{db_file}',
        modules={'models': ['models.models']},
        timezone="Asia/Shanghai"
    )
    # Generate the schema
    await Tortoise.generate_schemas()


async def close_db():
    await connections.close_all()

